package com.wanchuang.qhbb.module.partner;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.havi.util.StatusBarUtil;
import com.wanchuang.qhbb.R;
import com.wanchuang.qhbb.adapter.PartnerAdapter;
import com.wanchuang.qhbb.base.BaseActivity;
import com.wanchuang.qhbb.net.Apis;
import com.wanchuang.qhbb.net.NetworkManager;
import com.wanchuang.qhbb.net.WebApis;
import com.wanchuang.qhbb.net.interfaces.OnRequestCallBack;
import com.wanchuang.qhbb.proto.Mystock;
import com.wanchuang.qhbb.utils.ValidateUtils;
import com.wanchuang.qhbb.view.pinnedheader.PinnedHeaderItemDecoration;
import com.wanchuang.qhbb.view.pinnedheader.PinnedHeaderRecyclerView;
import com.wanchuang.qhbb.view.pinnedheader.SideBar;
import com.wanchuang.qhbb.web.ui.WebViewActivity;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PartnerActivity extends BaseActivity implements PartnerAdapter.OnItemClickListener {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycler_linear)
    PinnedHeaderRecyclerView recyclerLinear;
    @BindView(R.id.sidebar)
    SideBar sidebar;
    @BindView(R.id.tv_ti_shi)
    TextView tvTiShi;
    @BindView(R.id.empty_view)
    LinearLayout emptyView;

    @BindView(R.id.tv_total)
    TextView tv_total;
    @BindView(R.id.tv_zhi_jie)
    TextView tv_zhi_jie;
    @BindView(R.id.tv_jian_jie)
    TextView tv_jian_jie;

    private List<String> dataList = new ArrayList<>();
    private List<String> sortList = new ArrayList<>();
    private LinearLayoutManager layoutManager;
    private PartnerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_partner);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#FFFFFF"), true);
        ButterKnife.bind(this);
        tvTitle.setText("我的团队");
        backIv.setOnClickListener(v -> finish());
        initView();
        initData();
    }

    private void initView() {
        sidebar.setTextView(tvTiShi);
        layoutManager = new LinearLayoutManager(this);
        recyclerLinear.setLayoutManager(layoutManager);
        sidebar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                for (int i = 0; i < dataList.size(); i++) {
                    if (dataList.get(i).equals(s)) {
                        layoutManager.scrollToPositionWithOffset(i, 0);
                        break;
                    }
                }
            }
        });

    }

    private void initData() {
        NetworkManager.INSTANCE.post(Apis.get_partner_list, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                Mystock.merchant_list result = Mystock.merchant_list.parseFrom(bytes);
                Log.e("wang", "onOk: " + result.toString());
                List<Mystock.list_basis> list = result.getListDesList();
                List<Mystock.list_part> list_parts = result.getListPartList();
                StringBuilder des = new StringBuilder();
                for (int i = 0; i < list.size() - 1; i++) {
                    des.append(list.get(i).getName()).append("\n");
                }
                des.append(list.get(list.size() - 1).getName());
                for (int i = 0; i < list_parts.size(); i++) {
                    dataList.add(list_parts.get(i).getFirstCharter()+(i%2));
                    sortList.add(list_parts.get(i).getFirstCharter());
                    for (int j = 0; j < list_parts.get(i).getLisBasisList().size(); j++) {
                        dataList.add(list_parts.get(i).getLisBasisList().get(j).getName() + " " +
                                list_parts.get(i).getLisBasisList().get(j).getDescribe() + " " +
                                list_parts.get(i).getLisBasisList().get(j).getMobile()
                        );
                    }
                }
                tv_total.setText(result.getTotalNum());
                tv_zhi_jie.setText(result.getDirPartnerNum());
                tv_jian_jie.setText(result.getIndirPartnerNum());

                adapter = new PartnerAdapter(dataList, PartnerActivity.this);
                recyclerLinear.setAdapter(adapter);
                recyclerLinear.addItemDecoration(new PinnedHeaderItemDecoration());
                sidebar.setList(sortList);

                if (ValidateUtils.isValidate(dataList)) {
                    recyclerLinear.setVisibility(View.VISIBLE);
                    emptyView.setVisibility(View.GONE);
                } else {
                    recyclerLinear.setVisibility(View.GONE);
                    emptyView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onError(int code, String errorMessage) {
                showToast(errorMessage);
            }
        });
    }

    @Override
    public void onClick(View v, String position) {
        WebViewActivity.startActivity(mContext, WebApis.partner_detail + "?member_id=" + position);
    }
}
